/**
 * Created by python on 19-1-14.
 */
// /**
//  * Created by python on 19-1-12.
//  */
var vm = new Vue({
    el: "#app",
    delimiters: ['[[', ']]'], // 修改vue模板符号，防止与django冲突
    data: {
        // 页面中需要使用到的数据，键值对
        host: HOST,
        news_id:'',
        single_news: '',
        top_news: '',
        user_top_news:'',
        user_id:'',
        news_comments: ''

    },
    mounted: function () {
        this.news_id = this.get_query_string('news_id');
        axios.get(this.host + 'news/' + this.news_id,
         {
            news_id: this.news_id
        })
            .then(response => {
                this.single_news = response.data
                this.user_id = this.single_news.user.id
            })
            .catch(error => {
                console.log(error.response.data)
            });
        this.get_top_news();
        this.get_user_top_news()
        this.get_news_comments()


    },
    methods: {
        // 需要用到的函数，键值对 ，键是名称，值是匿名函数
         get_query_string: function(name){
            var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
            var r = window.location.search.substr(1).match(reg);
            if (r != null) {
                return decodeURI(r[2]);
            }
            return null;
        },

        // 获取网站阅读量最高的4篇文章
        get_top_news: function(){
            axios.get(this.host+'top_news/', {
                    responseType: 'json'
                })
                .then(response => {
                    this.top_news = response.data;

                })
                .catch(error => {
                    console.log(error.response.data);
                })
        },
        get_user_top_news: function(){
            this.news_id = this.get_query_string('news_id');
            axios.get(this.host+'user_top_news/'+ this.news_id, {
                    responseType: 'json'
                })
                .then(response => {
                    this.user_top_news = response.data;
                })
                .catch(error => {
                    console.log(error.response.data);
                })
        },
        get_news_comments: function(){
            this.news_id = this.get_query_string('news_id');
            axios.get(this.host+'news_comments/'+ this.news_id, {
                    responseType: 'json'
                })
                .then(response => {
                    this.news_comments = response.data;
                    console.log( this.news_comments[1].username)

                })
                .catch(error => {
                    console.log(error.response.data);
                })
        },


    }
})

